import Vue from "vue";
import iView from "view-design";
import vueBus from "vue-bus";

// 导入公用js
import "~/assets/js/common.js";

// 导入axios全局设置
import axiosUtil from "./axios-utils";

import "./vue-filters";

// 导入公用组件
import "../components/index";

import "view-design/dist/styles/iview.css";
import "~/assets/css/Site.less";

Vue.config.productionTip = false;

Vue.use(iView);
Vue.use(vueBus);

// iview加载进度条样式。
iView.LoadingBar.config({
  color: "#5cb85c",
  failedColor: "#f0ad4e",
  height: 6
});

// 获取浏览器客户端区域大小
Vue.prototype.getClientArea = () => {
  let clientAreaHeight = window.innerHeight - 105;

  return {
    width: window.innerWidth,
    height: clientAreaHeight
  };
};

function makeDeviceSign () {
  let matches = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);

  Vue.prototype.$isMobile = (matches || []).length > 0;
}

export default {
  init (app, router, store, restoreConfig, callback) {

    // 添加显示面包屑全局方法
    Vue.prototype.renderCrumbs = function (...items) {
      this.$store.commit("workbenchCrumbs", items);
    }

    // 重新设置高度
    Vue.prototype.resetSize = function () {
      let area = this.getClientArea();
      let container = document.querySelector("#appContainer");

      if (container) {
        container.style.height = area.height + "px";

        container.scroll({ top: 0 });

        this.$bus.emit("windowResizeChanged", area);
      }
    }

    if (typeof callback == 'function') {
      callback(Vue);
    }

    new Vue({
      router,
      store,
      render: h => h(app),
      created () {
        // 初始化axios
        axiosUtil.init(router, store, restoreConfig);

        this.resetSize();
        this.$Message.config({
          top: 10,
          duration: 4
        });

        makeDeviceSign();
      },
      mounted () {
        makeDeviceSign();
      }
    }).$mount("#app");

    window.onresize = () => {
      Vue.prototype.resetSize();
    }
  }
};
